<template>
    <div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            

            <el-form-item label="姓名" prop="UserName">
                <el-input v-model="ruleForm.UserName"></el-input>
            </el-form-item>
            
            <el-form-item label="性别" prop="Sex">
                <el-radio-group v-model="ruleForm.Sex">
                    <el-radio label="男" name="sex" ></el-radio>
                    <el-radio label="女" name="sex"></el-radio>
                </el-radio-group>
            </el-form-item>

            <el-form-item label="出生日期" required>
                <el-col :span="11">
                    <el-form-item prop="Birthday">
                        <el-date-picker value-format="yyyy-MM-dd" placeholder="选择日期" v-model="ruleForm.Birthday"
                            style="width: 100%;"></el-date-picker>
                    </el-form-item>
                </el-col>
            </el-form-item>

            <el-form-item label="手机号码" prop="Phone">
                <el-input v-model="ruleForm.Phone"></el-input>
            </el-form-item>
           
           
            <el-form-item>
                <el-tooltip class="item" effect="dark" content="修改数据" placement="top-start">
                <el-button type="primary" icon="el-icon-edit" circle @click="submitForm('ruleForm')"></el-button>
            </el-tooltip>
            <el-tooltip class="item" effect="dark" content="清空数据" placement="right-start">
                <el-button type="danger" icon="el-icon-delete" circle @click="resetForm('ruleForm')"></el-button>
            </el-tooltip>
            </el-form-item>
        </el-form>


  
  
    </div>
    
</template>

<script>
import { mapState } from 'vuex'
export default {
    name:'add',
    data(){
        return {
          
            ruleForm: {
                UserId:0,
                UserName: "狗华",
                Sex:"男",
                Birthday: "",              
                Phone: "",
               
            },
            rules: {
                UserName: [
                    { required: true, message: '请输入用户信息', trigger: 'blur' },
                    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
                ],
                
                Birthday: [
                    {  required: true, message: '请选择日期', trigger: 'change' }
                ],
                Sex: [
                    { required: true, message: '请选择性别', trigger: 'change' }
                ],
                Phone: [
                    { required: true, message: '请输入手机号码', trigger: 'blur' },
                    { min: 11, max: 11, message: '长度在必须为11位', trigger: 'blur' }
                ],
                
            }
        }
    },
    props: {
       xian: Boolean,
       shuju:Object
    },
    computed:{
        ...mapState(['user'])
    },
    methods:{
        submitForm(formName) {
            //先验证表单数据
            this.$refs[formName].validate((valid) => {
                if (valid) {
                  
               this.axios({
                method:'PUT',
                url:'User/gai',
                data:this.ruleForm
            }).then(res=>{
                if(res.code>0){
                 
                  this.$emit('update:xian',false)
                  this.$emit('refresh');
                  this.user.UserName=this.ruleForm.UserName
                    this.$message({ type:'success', message:res.msg})

                }else{
                    this.$message({ type:'danger', message:res.msg})
                }
            })
             
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
       
    },
    created(){       
        this.ruleForm.UserID=this.ruleForm.UserId
        this.ruleForm=this.shuju
    },
   
}
</script>

<style scoped>

</style>